package com.bsj.travel.auth.service;

import com.bsj.travel.constant.SecurityConstants;
import com.bsj.travel.def.global.JsonResult;
import com.bsj.travel.def.global.JsonResultEnum;
import com.bsj.travel.exception.ApiException;
import com.bsj.travel.system.RemoteUserService;
import com.bsj.travel.def.common.VO.SystemUser;
import com.bsj.travel.def.common.VO.TokenUser;
import com.bsj.travel.util.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author yinhao
 * @version 1.0
 * @description 登录方法
 * @date 2023/11/2
 */
@Component
public class SystemLoginService {

    @Resource
    private RemoteUserService remoteUserService;

    /**
     * 系统登录
     * @param userName 用户名
     * @param passWord 密码
     * @return
     */
    public TokenUser login(String userName, String passWord) {
        if (StringUtils.isAnyBlank(userName, passWord)) {
            throw new ApiException(JsonResultEnum.ACCOUNT_PASSWORD_NULL);
        }
        JsonResult<TokenUser> userResult = remoteUserService.queryUserByName(userName, SecurityConstants.INNER);
        if (JsonResultEnum.NOT_USER_INFO_ERROR.getCode() == userResult.getCode()) {
            throw new ApiException(JsonResultEnum.USER_INFO_ISNULL);
        }
        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
            throw new ApiException(JsonResultEnum.USER_INFO_ISNULL);
        }
        TokenUser tokenUser = userResult.getData();
        SystemUser systemUser = tokenUser.getSystemUser();
        if (!StringUtils.equals(passWord, systemUser.getPassWord())) {
            throw new ApiException(JsonResultEnum.USER_INFO_ERROR);
        }
        return tokenUser;
    }
}
